{template 'web/_header'}
{template 'tabs'}
{if $operation == 'post'}

<script type="text/javascript" src="resource/js/lib/jquery-ui-1.10.3.min.js"></script>
<div class="main">
    <form action="" method="post" class="form-horizontal form" enctype="multipart/form-data">
        <input type="hidden" name="tp_id" value="{$item['id']}" />
        <div class='panel panel-default'>
            <div class='panel-heading'>{if empty($_GPC['id'])}新建{else}编辑 (id:{$_GPC['id']}){/if} - 自定义表单模板 - 拖动可改变字段位置</div>
            <div class='panel-body'>
                {ife 'diyform.temp' $item}
                {if !empty($_GPC['id'])}
                <div class="alert alert-info">警告：当模板中已经添加数据后改变模板结构有可能导致无法使用！
                    <br/>使用情况：
                    <br/>会员资料 ({if $use_flag1}正在使用{else}未使用{/if})
                    <br/>分销商申请资料 ({if $use_flag2}正在使用{else}未使用{/if})
                    <br/>商城商品({if $datacount3>0}{php echo $datacount3}种商品正在使用{else}未使用{/if})</div>
                {/if}
                {/if}

                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label" > 分类</label>
                    <div class="col-sm-9 col-xs-12" style="width:707px;">
                        {ife 'diyform.temp' $item}
                        <select name="cate" class="form-control">
                            <option value=""></option>
                            {loop $category $c}
                            <option value="{$c['id']}" {if $item['cate']==$c['id']}selected{/if}>{$c['name']}</option>
                            {/loop}
                        </select>
                        {else}
                        {loop $category $c}
                        {if $item['cate']==$c['id']}{$c['name']}{/if}
                        {/loop}
                        {/if}
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label" ><span style='color:red'>*</span> 模版名称</label>
                    <div class="col-sm-9 col-xs-12" style="width:707px;">
                        {ife 'diyform.temp' $item}
                        <input type="text" name="tp_title" class="form-control tp_title" value="{$item['title']}" placeholder="模版名称，例：用户资料" />
                        {else}
                        <div class='form-control-static'>{$item['title']}</div>
                        {/if}
                    </div>
                </div>


                <div id="type-items">

                    {php $kw=0;}
                    {loop $item['fields'] $k1 $v1}
                    {php $data_type = $v1['data_type'];}
                    {if $datacount>0}
                    {php $flag=2;}
                    {/if}

                    {template 'tp_type'}
                    {php $kw++;}
                    {/loop}


                </div>

                {ife 'diyform.temp' $item}

                <div class="form-group">
                    <label class="col-xs-12 col-sm-3 col-md-2 control-label" >要增加的字段类型</label>
                    <div class="col-sm-9 col-xs-12">
                        {ife 'diyform.temp' $item}
                        <select id="data_type" name="data_type" class="form-control" style="width:187px;display:inline;">
                            {loop $data_type_config $key $value}
                            <option value="{$key}">{$value}</option>
                            {/loop}
                        </select>
                        {else}
                        {loop $category $c}
                        {if $item['cate']==$c['id']}{$c['name']}{/if}
                        {/loop}
                        {/if}

                        <a class="btn btn-default btn-add-type" href="javascript:;" onclick="addType();"><i id="add_field" class="fa fa-plus" title=""></i> 增加一个字段</a>
                    </div>
                </div>
            </div>

            {/if}

            <div class="form-group">
                <label class="col-xs-12 col-sm-3 col-md-2 control-label" ></label>
                <div class="col-sm-9 col-xs-12">
                    {ife 'diyform.temp' $item}
                    <input type="submit" name="submit" value="提交" class="btn btn-primary col-lg-1"  />
                    <input type="hidden" name="token" value="{$_W['token']}" />
                    {/if}
                    <a href="{php echo $this->createPluginWebUrl('diyform')}"  {ife 'diyform.temp' $item}style='margin-left:10px;'{/if}><span class="btn btn-default" style='margin-left:10px;'>返回列表</span></a>
                </div>
            </div>
        </div>
</div>
</form>
</div>

<script language='javascript'>

    $(function(){
        load_datetimepicker();

        $('#type-items').sortable({
            stop: function(){
                window.optionchanged = true;
            }
        });
    })


    function load_datetimepicker() {
        require(["datetimepicker"], function(){
            $(function(){
                $(".datetimepicker").each(function(){
                    var option = {
                        lang : "zh",
                        step : "10",
                        timepicker : false,
                        closeOnDateSelect : true,
                        format : "Y-m-d"};
                    $(this).datetimepicker(option);
                });
            });
        });
    }


    function tp_change_default(knum){
        if ($("#tp_is_default"+knum).val() == 1) {
            $("#tp_default"+knum).css("display","inline");
        } else {
            $("#tp_default"+knum).hide();
        }
    }

    function tp_change_default_time(obj,ids){
        if (obj.value == 2) {
            $("#"+ids).css("display","inline");

        } else {
            $("#"+ids).hide();
        }
    }


    var kw = {php echo $kw};
    function addType() {
        var data_type = $("#data_type").val();

        $(".btn-add-type").button("loading");
        $.ajax({
            url: "{php echo $this->createPluginWebUrl('diyform/temp',array('op'=>'addtype','addt'=>'type'))}&flag=1&kw="+kw+"&data_type="+data_type,
            cache: false
        }).done(function (html) {
            $(".btn-add-type").button("reset");
            $("#type-items").append(html);
            if (data_type == 7 || data_type ==8){
                load_datetimepicker();
            }
        });
        kw++;
    }

    function removeType(obj) {
        $(obj).parent().parent().parent().parent().remove();
    }

    $('form').submit(function(){
        var check = true;
        //$("input[type=text]").each(function(){
        $(".tp_title,.tp_name").each(function(){
            var val = $(this).val();
            if(!val){
                Tip.focus($(this),'不能为空!');
                check =false;
                return false;
            }
        });

        if(kw == 0) {
            Tip.focus($("#add_field"),'请先添加字段再提交!');
            check =false;
            return false;
        }

        if(!check){return false;}
        var o={}; // 判断重复
//        $("input[mk=1]").each(function(){
//            if(!(o[$(this).val()])){
//                o[$(this).val()] = true;
//            }else{
//                var val = $(this).val();
//                $("input[mk=1]").each(function(){
//                    if($(this).val()==val){
//                        $(this).css("border-color","#f01");
//                    }else{
//                        $(this).css("border-color","#ccc");
//                    }
//                });
//                alert("啊哦，红圈里的数据 不能重复哦~！");
//                check =false;
//                return false;
//            }
//        });
        if(!check){return false;}
        return check;
    });

</script>

{elseif $operation == 'display'}
<!-- 筛选区域 -->
<div class="panel panel-info">
    <div class="panel-heading">筛选</div>
    <div class="panel-body">
        <form action="./index.php" method="get" class="form-horizontal" role="form">
            <input type="hidden" name="c" value="site" />
            <input type="hidden" name="a" value="entry" />
            <input type="hidden" name="m" value="sz_yi" />
            <input type="hidden" name="do" value="plugin" />
            <input type="hidden" name="p" value="diyform" />
            <input type="hidden" name="method" value="temp" />
            <input type="hidden" name="op" value="display" />
            <div class="form-group">
                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">关键字</label>
                <div class="col-sm-8 col-lg-9">
                    <input class="form-control" name="keyword"  type="text" value="{$_GPC['keyword']}" placeholder="请输入模板名称进行搜索">
                </div>
            </div>
            <div class="form-group">
                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label"></label>
                <div class="col-sm-8 col-lg-9">
                    <button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class='panel panel-default'>
    <div class='panel-heading'> 自定义表单模版列表</div>
    <div class='panel-body'>
        {if empty($items)}
        {if empty($_GPC['keyword'])}
        <p style="line-height: 40px;">还没有添加模板哦~</p>
        {else}
        <p style="line-height: 40px;">未检索到与“{$_GPC['keyword']}”相关的模板</p>
        {/if}
        {else}
        <table class="table">
            <thead>
            <tr>
                <th style='width:100px;'>ID</th>
                <th >模版名称</th>
                <!--<th style="width:200px">已使用/总共数据</th>-->
                <th style="width:200px">使用情况(正在使用)</th>
                <th >操作</th>
            </tr>
            </thead>
            <tbody>
            {loop $items $item}
            <tr>
                <td>{$item['id']}</td>
                <td><label class='label label-primary'>{php echo $category[$item['cate']]['name']}</label> {$item['title']}</td>
                <td>
                    {if $item['use_flag1']}
                        会员资料
                    {/if}
                    {if $item['use_flag2']}
                        分销商申请资料
                    {/if}
                    {if $item['datacount3']}
                        {php echo $item['datacount3']}种商品
                    {/if}
                </td>
                <!--td>
                    {ifp 'diyform.data.view'}
                    <a href="{php echo $this->createPluginWebUrl('diyform/data', array('typeid'=>$item['id']))}" title="点击查看/编辑">{$item['count_num']} 详细</a>


                    {else}
                    {$item['count_num']}
                    {/if}
                </td-->
                <td>
                    {ifp 'diyform.temp.edit|diyform.temp.view'}<a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform/temp', array('op' => 'post', 'id' => $item['id']))}"><i class='fa fa-edit'></i></a>{/if}
                    {ifp 'diyform.data.view'}<a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform/data', array('typeid' => $item['id']))}" title='查看已有数据'><i class='fa fa-list'></i></a>{/if}
                    {ifp 'diyform.data.add'}<!--a class='btn btn-primary' href="{php echo $this->createPluginWebUrl('diyform/data', array('op' => 'post', 'typeid' => $item['id']))}" title='添加数据'><i class='fa fa-plus'></i></a-->{/if}
                    {ifp 'diyform.temp.edit'}<!--a class='btn btn-default'  href="{php echo $this->createPluginWebUrl('diyform/temp', array('op' => 'delete', 'id' => $item['id']))}" onclick="return confirm('确认删除此模版吗？');return false;" title='删除模板'><i class='fa fa-remove'--></i></a>{/if}
                    {ifp 'diyform.temp.edit'}<a class='btn btn-default'  href="javascript:void(0)" onclick="del_temp({php echo $item['id']})" title='删除模板'><i class='fa fa-remove'></i></a>{/if}
                </td>
            </tr>
            {/loop}
            {if !empty($pager)}
            <tr>
                <td colspan="4">{$pager} </td>
            </tr>
            {/if}
            </tbody>
        </table>
    </div>
    {/if}
    {ifp 'diyform.temp.add'}
    <div class='panel-footer'>
        <a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform/temp', array('op' => 'post'))}"><i class="fa fa-plus"></i> 添加新模版</a>
        <span style="line-height:32px;background-size:13583px; font-size:12px; margin-left: 10px;">温馨提示：点击数据可以查看哦~~</span>
    </div>
    {/if}
</div>

<script language="javascript">

function del_temp(id) {
    if(confirm('确认删除此模版吗？'))
    {
        $.ajax({
            type: "post",
            url: "{php echo $this->createPluginWebUrl('diyform/temp', array('op' => 'delete'))}",
            data: "id="+id,
            dataType: "json",
            success: function (json) {
                var result = json.result;
                if(json.status==1){
                    location.href = "{php echo $this->createPluginWebUrl('diyform/temp')}";
                } else {
                    alert(result);
                }
            }
        });

    }
}
</script>



{else if $operation == 'addtype'}
<div class="form-group">
    <label class="col-xs-12 col-sm-3 col-md-2 control-label" ><span style='color:red'>*</span></label>
    <div class="col-sm-9 col-xs-12" style="width:5%">
        <a class="btn btn-default" href='javascript:;' onclick='removeType(this)'><i class='icon icon-remove fa fa-times'></i> 删除</a>
    </div>
    <div class="col-sm-9 col-xs-12" style="width:15%">
        <input type="text" name="tp_kw[]" class="form-control" value="{$group['groupname']}" placeholder="键值，例：keywords{$kw}" />
    </div>
    <div class="col-sm-9 col-xs-12" style="width:40%">
        <input type="text" name="tp_value[]" class="form-control" value="{$group['groupname']}" placeholder="请在此输入对应的值" />
    </div>
    <div class="col-sm-9 col-xs-12" style="width:30%; ">
        {php echo tpl_form_field_color('tp_color[]', $value=$settings['maincolor'])}
    </div>
</div>
{elseif $operation == 'postdata'}
<form id="dataform" action="" method="post" class="form-horizontal form" enctype="multipart/form-data">
    <div class='panel panel-default'>
        <div class='panel-heading'>{if empty($_GPC['id'])}添加数据 (模板id:{$_GPC['typeid']}){else}编辑数据 (模板id:{$_GPC['typeid']} 数据id:{$_GPC['id']}){/if}</div>
        <input type="hidden" name="typeid" value="{$item['id']}"/>
        <div class='panel-body'>
            <div class="alert alert-danger">{if empty($_GPC['id'])}您正在向模板:“{$item['title']} (id:{$item['id']})” 添加数据{else}您正在编辑模板id:{$_GPC['typeid']}数据id:{$_GPC['id']}的内容{/if}<br>Tips:主键自动填充只适用于以下格式：10000001(纯数字)、C00000001(一位字母开头的数字) 其他格式请手动填写或者Excel导入。</div>
            <table class="table">
                <thead>
                <tr>
                    {loop $item['fields'] $fields}
                    <th>{$fields['name']} ({$fields['keyword']}) {if $fields['keyword']=='key'}主键 {if empty($_GPC['id'])}<a style="float: right;" href="javascript:;" onclick="autonum()">自动填充</a>{/if}{/if}</th>
                    {/loop}
                    <th style="width: 50px;">操作</th>
                </tr>
                </thead>
                <tbody id="type-items">
                {template 'tp_data'}
                </tbody>
            </table>
            {if empty($_GPC['id'])}
            <div class="input-group " style="width:260px; float: left; margin-left: 8px;">
                <span class="input-group-addon">增加</span>
                <input type="text" name="numlist" value="1" class="form-control" style="padding:0px; text-align: center;">
                <span class="input-group-addon" style="border-left: 0px;">条数据</span>
                <span class="btn btn-default btn-add-type btn-add-type2" style="float: left; border-radius: 0px 4px 4px 0px; border-left: 0px;" onclick="addType();"><i class="fa fa-plus" title="" ></i> 确认增加</span>
            </div>
            <div class="col-sm-9 col-xs-12" style="float: left; width: auto; ">
                <a class="btn btn-default btn-add-type" href="javascript:;" onclick="autonum()" style="margin-right: 10px;"><i class="fa fa-angle-double-down" title=""></i> 主键自动填充</a>
                <a class="btn btn-primary" href="javascript:;" style="margin-right: 10px;" onclick="$('#modal-import').modal()"><i class="fa fa-plus" title=""></i> Excel导入</a>
                <a class="btn btn-primary" href="{php echo $this->createPluginWebUrl('diyform/import',array('op'=>'temp','id'=>$item['id']))}" style="margin-right: 10px;" ><i class="fa fa-download" title=""></i> 下载Excel模板文件</a>
            </div>
            {/if}
        </div>
        {ifp 'diyform.temp.add'}
        <div class='panel-footer' style="height:auto; overflow: hidden;">
            <input type="submit" name="submit" value="保存数据" class="btn btn-primary col-lg-1"  />
            <a class="btn btn-default btn-add-type" href="{php echo $this->createPluginWebUrl('diyform',array('op'=>'list','typeid'=>$item['id']))}" style="margin-left: 10px;"><i class="fa fa-list" title=""></i> 查看已有数据</a>
            <input type="hidden" name="token" value="{$_W['token']}" />
        </div>
        {/if}
    </div>
</form>

<div id="modal-import" class="modal fade" tabindex="-1" role="dialog" aria-hidden="true" style="width:600px;margin:0px auto;">
    <form id="importform" class="form-horizontal form" action="{php echo $this->createPluginWebUrl('diyform/import')}" method="post" enctype="multipart/form-data">
        <input type='hidden' name='typeid' value="{$item['id']}" />
        <input type='hidden' name='op' value='import' />

        <div class="modal-dialog">
            <div class="modal-content">
                <div class="modal-header">
                    <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                    <h3>上传数据</h3>
                </div>
                <div class="modal-body">
                    <div class="form-group">
                        <label class="col-xs-12 col-sm-3 col-md-2 control-label" style='width: 150px'>选择EXCEL:</label>
                        <div class="col-sm-9 col-xs-12" style='width: 380px'>
                            <input type="file" name="excelfile" class="form-control" />
                        </div>
                        <label class="col-xs-12 col-sm-3 col-md-2 control-label" style='width: 150px'>注意:</label>
                        <div class="col-sm-9 col-xs-12" style='width: 380px'>
                            <span style="line-height: 36px; font-size: 12px;">如果遇到数据重复则将进行数据更新（在数据未使用的情况下）</span>
                        </div>
                    </div>
                    <div id="module-menus"></div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-primary span2" name="cancelsend" value="yes">确认导入</button>
                    <a href="#" class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</a>
                </div>
            </div>
        </div>
    </form>
</div>
<script language='javascript'>
    $(function(){

        $('#importform').submit(function(){
            if(!$(":input[name=excelfile]").val()){
                alert("您还未选择Excel文件哦~");
                return false;
            }
        })

        $('#dataform').submit(function(){
            var check = true;
            $("input[type=text]").each(function(){
                var val = $(this).val();
                if(!val){
                    Tip.focus($(this),'不能为空!');
                    check =false;
                    return false;
                }
            });
            if(!check){return false;}
            var o={}; // 判断重复
            $("input[mk=1]").each(function(){
                if(!(o[$(this).val()])){
                    o[$(this).val()] = true;
                }else{
                    var val = $(this).val();
                    $("input[mk=1]").each(function(){
                        if($(this).val()==val){
                            $(this).css("border-color","#f01");
                        }else{
                            $(this).css("border-color","#ccc");
                        }
                    });
                    alert("啊哦，红圈里的数据 不能重复哦~！");
                    check =false;
                    return false;
                }
            });
            if(!check){return false;}
            return check;
        });
    })
    var kw = 1;
    function addType() {
        numlist = $("input[name=numlist]").val();
        if(numlist>50){
            alert("每次最多增加50条数据哦~");
            return false;
        }
        $(".btn-add-type2").button("loading");
        $.ajax({
            url: "{php echo $this->createPluginWebUrl('diyform',array('op'=>'addtype','addt'=>'data','typeid'=>$_GPC['typeid']))}&kw="+kw+"&numlist="+numlist,
            cache: false
        }).done(function (html) {
            $(".btn-add-type2").button("reset");
            $("#type-items").append(html);
        });
        kw++;
    }
    function removeType(obj) {
        $(obj).parent().parent().remove();
    }

    function autonum(){
        var val = $("input[mk=1]").val();
        var num =val.replace(/[^0-9]/,'')
        var num2 = 1+num;
        var eng = val.replace(num,'');


        if(!val){
            Tip.focus($("input[mk=1]"),'请先录入一个值!');
            reurun;
        }
        $("input[mk=1]").each(function(i){
            if(i>0){
                vval = 1+parseInt(num2)+i;
                str= ""+vval;
                str = str.substring(1,str.length)
                $(this).val(eng+str);
            }
        });
    }
</script>

{elseif $operation == 'list'}
<!-- 筛选区域 -->
<div class="panel panel-info">
    <div class="panel-heading">筛选</div>
    <div class="panel-body">
        <form action="./index.php" method="get" class="form-horizontal" role="form">
            <input type="hidden" name="c" value="site" />
            <input type="hidden" name="a" value="entry" />
            <input type="hidden" name="m" value="sz_yi" />
            <input type="hidden" name="do" value="plugin" />
            <input type="hidden" name="p" value="diyform" />
            <input type="hidden" name="op" value="list" />
            <input type="hidden" name="typeid" value="{$type['id']}" />
            <div class="form-group">
                <label class="col-xs-12 col-sm-2 col-md-2 col-lg-1 control-label">关键字</label>
                <div class="col-sm-8 col-lg-9">
                    <input class="form-control" name="keyword" id="" type="text" value="{$_GPC['keyword']}" placeholder="请输入主键(key)进行搜索">
                </div>
                <div class=" col-xs-12 col-sm-2 col-lg-2">
                    <button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
                </div>
            </div>
        </form>
    </div>
</div>
<div class='panel panel-default'>
    <div class='panel-heading'> 数据列表(总数:{$total}; 模板名称:{$type['title']})</div>
    <div class='panel-body'>
        <table class="table">
            <thead>
            <tr>
                <th style='width:50px;'>编号</th>
                {php $colsoan=0}
                {loop $type['fields'] $fields}
                <th style='text-align: center;'>{$fields['name']} ({$fields['keyword']}) {php $colspan++}</th>
                {/loop}
                <th style='text-align: center;'>状态</th>
                <th>购买者</th>
                <th>购买时间</th>
                <th>订单号</th>
                <th>购买价格</th>
                <th>辑编 / 删除</th>
            </tr>
            </thead>
            <tbody>
            {loop $items $item}
            <tr>
                <td>{$item['id']}</td>
                {php $datas = json_decode($item['fields'],ture)}
                {loop $datas $data}
                <td style='text-align: center;'>
                    <?php foreach($data as $key => $val){ echo '<p style="padding:0px; margin:0px; width:auto;" title="'.$data[$key].'">'.$data[$key].'</p>'; } ?>
                </td>
                {/loop}
                <td style='width:60px; text-align: center'>
                    {if empty($item['openid'])}<span style="color:green">未使用</span>{else}<span style="color:red;">已使用</span>{/if}
                </td>
                <td>
                    {if empty($item['openid'])}<span style="width: 100%">-</span>{else}<p style="padding:0px; margin:0px; width:auto;" title="{$item['openid']}">{$item['openid']}</p>{/if}
                </td>
                <td>
                    {if empty($item['openid'])}<span style="width: 100%">-</span>{else}{$item['usetime']}{/if}
                </td>
                <td>
                    {if empty($item['openid'])}<span style="width: 100%">-</span>{else}{$item['orderid']}{/if}
                </td>
                <td>
                    {if empty($item['openid'])}<span style="width: 100%">-</span>{else}{$item['price']}{/if}
                </td>
                <td>
                    {if empty($item['openid'])}
                    {ifp 'diyform.temp.edit|diyform.temp.view'}<a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform', array('op' => 'postdata', 'id' => $item['id'],'typeid'=>$item['typeid']))}" title="{ifp 'diyform.temp.edit'}编辑{else}查看{/if}"><i class='fa fa-edit'></i></a>{/if}
                    {ifp 'diyform.temp.edit'}<a class='btn btn-default'  href="{php echo $this->createPluginWebUrl('diyform', array('op' => 'deletedata','typeid'=>$item['typeid'],'id' => $item['id']))}" onclick="return confirm('确认删除此条数据吗？');return false;" title='删除'><i class='fa fa-remove'></i></a>{/if}
                    {else}
                    <span style="width: 100%;">-</span>
                    {/if}
                </td>
            </tr>
            {/loop}
            {if !empty($pager)}
            <tr>
                <td colspan="{php echo $colspan+7}">{$pager}</td>
            </tr>
            {/if}
            </tbody>
        </table>
    </div>
    <div class='panel-footer'>
        <a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform')}"><i class="fa fa-reply"></i> 返回列表</a>
        <a class='btn btn-default' href="{php echo $this->createPluginWebUrl('diyform', array('op' => 'postdata','typeid'=>$_GPC['typeid']))}"><i class="fa fa-plus"></i> 添加数据</a>
    </div>
</div>

{/if}
{template 'common/footer'}
